import fetcher from "../apis/Fetcher";
import {NumberIdAndName} from "../types/BasicModels";
import {AddOrderItemKey, QueryGoodsUnitResponse, SubGoodsResponse} from "./GoodsApi";
import {Page, Pageable} from "../utils/Page";
import {OffsetDateTime} from "@js-joda/core";
import {ProcurementItemInOrderSummary} from "./ProcurementApi";

export function getProcurementReceiptDetail(id: number) {
    return fetcher.get<ProcurementReceiptDetail>(`procurementSuppliers/receipts/${id}`, {
        includeItems: true,
        includeComparable:true,
    })
}

export interface QueryMarketOrderRequest {
    supplierIds: number[]
    startCreatedDate?: string
    endCreatedDate?: string
    validatedStartDate?: string
    validatedEndDate?:string
}

export function queryProcurementReceipts(queryMarketOrderRequest: QueryMarketOrderRequest, pageable: Pageable) {
    return fetcher.get<Page<QueryProcurementReceiptResponse>>("procurementSuppliers/receipts", {...queryMarketOrderRequest, ...pageable})
}

export interface QueryProcurementReceiptResponse{
    id:number
    createdDateTime: OffsetDateTime
    supplier: NumberIdAndName
    validatedDateTime?: OffsetDateTime
    procurementId?:number
    goodsKindAmount:number
}

export function getProcurementReceiptDetailWithoutItems(id: number) {
    return fetcher.get<ProcurementReceiptDetail>(`procurementSuppliers/receipts/${id}`)
}



export interface ProcurementReceiptDetail extends QueryProcurementReceiptResponse{

    items: Array<ProcurementReceiptItem>
    note?:string
}


export interface ProcurementReceiptItem {
    id: number
    name: string
    barcode: string,
    amountInOrder: number
    inStore: number
    boxSpecification?: string
    units: QueryGoodsUnitResponse[]
    subGoods?: SubGoodsResponse
    unit?: NumberIdAndName
    note?: string
    practicalAmount?:number
    amount:number
    price:number
}

export interface AddProcurementReceiptItemRequest {
    amount: number
    goodsId: number
}

export function addProcurementReceiptItem(id: number, addProcurementReceiptItemRequest: AddProcurementReceiptItemRequest) {
    return fetcher.post(`procurementReceipts/${id}/items`, {}, addProcurementReceiptItemRequest)
}

export function addProcurementReceiptItemFromBarcode(id:number,barcode:string){
    return fetcher.post<ProcurementReceiptItemInOrderSummary>(`/procurementReceipts/${id}/items/barcodes/${barcode}`)
}

export interface ProcurementReceiptItemInOrderSummary{
    goodsId: number,
    name: string,
    barcode: string,
    amountInOrder: number,
    inStore: number,
    boxSpecification: string,
    units: QueryGoodsUnitResponse[],
    unit?: NumberIdAndName,
    subGoods?: SubGoodsResponse,
    price: number
    practicalAmount?:number
}

export function deleteProcurementReceiptItem(orderId: number, goodsId: number) {
    return fetcher.delete(`procurementReceipts/${orderId}/items/${goodsId}`)
}

export function deleteProcurementReceiptItemSub(orderId: number, goodsId: number,subGoodsId:number) {
    return fetcher.delete(`procurementReceipts/${orderId}/items/${goodsId}/sub/${subGoodsId}`)
}

export interface UpdateMarketOrderItemAmountRequest {
    amount: number
    unitId?:number
}

export function updateProcurementReceiptItemAmount(orderId: number, goodsId: number, updateMarketOrderItemAmountRequest: UpdateMarketOrderItemAmountRequest) {
    return fetcher.patch(`procurementReceipts/${orderId}/items/${goodsId}/amount`, {}, updateMarketOrderItemAmountRequest)
}

export function updateProcurementReceiptItemSubAmount(orderId: number, goodsId: number, subGoodsId:number, updateMarketOrderItemAmountRequest: UpdateMarketOrderItemAmountRequest) {
    return fetcher.patch(`procurementReceipts/${orderId}/items/${goodsId}/sub/${subGoodsId}/amount`, {}, updateMarketOrderItemAmountRequest)
}

export function updateProcurementReceiptItemNote(orderId: number, goodsId: number, note: string) {
    return fetcher.patch(`procurementReceipts/${orderId}/items/${goodsId}/note`, {}, note)
}

export function updateProcurementReceiptItemSubNote(orderId: number, goodsId: number, note: string, subGoodsId: number) {
    return fetcher.patch(`procurementReceipts/${orderId}/items/${goodsId}/sub/${subGoodsId}/note`, {}, note)
}


// export type ProcurementReceiptStatus = keyof typeof procurementReceiptStatusMap
//
// export const procurementReceiptStatusMap =
//     {
//         "WAIT_VALIDATE": "待验收",
//         "VALIDATED": "已验收",
//         "AUDITED": "已审核",
//     } as const
//
// export const procurementReceiptStatuses = Object.keys(procurementReceiptStatusMap) as ProcurementReceiptStatus[]


export interface ProcurementReceiptCompareWithOriginalOrderItem{
    goodsId:number
    goodsName:string
    goodsBarcode:string
    amount?:number
    actuallyAmount?:number
}

export function validateProcurementReceipt(orderId:number){
    return fetcher.patch(`procurementReceipts/${orderId}/validate`)
}

export function deleteProcurementReceipt(orderId:number){
    return fetcher.delete(`procurementReceipts/${orderId}`)
}

export function downloadImportProcurementReceiptItemsTemplate(){
    return fetcher.get<Blob>("procurementSuppliers/receipts/items/excel/import/template", {}, {responseType: 3})
}

export function importProcurementReceiptItemFromExcel(orderId:number,file: File) {
    let formData = new FormData();
    formData.append("file", file)
    return fetcher.post<number>(`procurementReceipts/${orderId}/items/excel/import`, {}, formData, {
        timeout: 30000
    })
}

export function updateProcurementReceiptNote(id:number,note:string){
    return fetcher.patch(`procurementReceipts/${id}/note`, {}, {note})
}

export function addProcurementReceiptItemsFromSubGoodsKeyList(orderId: number, orderItemKeys: AddOrderItemKey[]) {
    return fetcher.post<ProcurementReceiptItemInOrderSummary[]>(`/procurementReceipts/${orderId}/items/batchAdd`, {}, orderItemKeys)
}

export interface ValidateItemRequest{
    amount:number
}

export function validateProcurementReceiptItem(orderId: number, goodsId: number, request:ValidateItemRequest) {
    return fetcher.patch(`procurementReceipts/${orderId}/items/${goodsId}/validate`, {}, request)
}

export function validateProcurementReceiptItemSub(orderId: number, goodsId: number,  subGoodsId: number,request:ValidateItemRequest) {
    return fetcher.patch(`procurementReceipts/${orderId}/items/${goodsId}/sub/${subGoodsId}/validate`, {}, request)
}